<?php
/**
 * Akimimi front
 * BaseView
 * by M.Wang
 */
class BaseView{

	/**
	 * @var string like /n/javascript/
	 */
    public $jsPathPublic;
    /**
     * @var string like /t/default_zh_cn/javascript/
     */
    public $jsPathPrivate;
    /**
     * @var string like /n/css/
     */
    public $cssPathPublic;
    /**
     * @var string like /t/default_zh_cn/css/
     */
    public $cssPathPrivate;
    /**
     * @var string like /n/image/
     */
    public $imagePathPublic;
    /**
     * @var string like /t/default_zh_cn/image/
     */
    public $imagePathPrivate;
    /**
     * @var array
     */
	protected $js = array();
	/**
	 * @var array
	 */
	protected $css = array();
	/**
	 * @var array
	 */
	protected $meta = array();
	/**
	 * @var string
	 */
	protected $title = '';
	/**
	 * @var string
	 */
	protected $headExtends = '';

	public function __construct(){
		$str_language = $GLOBALS['config']['template_name'];
        $this->jsPathPublic = '//'.ConfigConstants::$iConstants.'.'.DOMAIN.'/n/javascript/';
        $this->jsPathPrivate = '//'.ConfigConstants::$iConstants.'.'.DOMAIN.'/t/'.$str_language.'/javascript/';
        $this->cssPathPublic = '//'.ConfigConstants::$iConstants.'.'.DOMAIN.'/n/css/';
        $this->cssPathPrivate = '//'.ConfigConstants::$iConstants.'.'.DOMAIN.'/t/'.$str_language.'/css/';
        $this->imagePathPublic = '//'.ConfigConstants::$iConstants.'.'.DOMAIN.'/n/image/';
        $this->imagePathPrivate = '//'.ConfigConstants::$iConstants.'.'.DOMAIN.'/t/'.$str_language.'/image/';
        $this->js[] = $this->jsPathPublic . 'lib/jquery.js';
        $this->js[] = $this->jsPathPrivate . 'akimimi.js';
        $this->css[] = $this->cssPathPrivate . 'default.css';
        $this->css[] = $this->cssPathPrivate . 'headerAndFooter.css';
        $this->meta[] = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
        $this->meta[] = "<link rel='shortcut icon' href='" . ConfigConstants::getTImagePath() . "/favicon.ico'></link>";
	}

	protected function getJsIncludes(){
		if( empty( $this->js ) )
            return '';
		$str_setting = "<script type='text/javascript' language='javascript' src='%s'></script>";
		$str_result = '';
		foreach ( $this->js as $str_file )
            $str_result.= sprintf( $str_setting, $str_file );
        return $str_result;
	}

	protected function getCssIncludes(){
		if( empty( $this->css ) )
            return '';
        $str_setting = "<link href='%s' rel='stylesheet' type='text/css' />";
        $str_result = '';
        foreach ( $this->css as $str_file )
            $str_result.= sprintf( $str_setting, $str_file );
        return $str_result;
	}

	/**
	 * @param string $str_filename base on public path or private path setting
	 * @param boolean $bool_isPublic default is false [base on private path]
	 * @return true
	 */
	public function addJS($str_filename, $bool_isPublic=false){
		if( $bool_isPublic )
            $str_file = $this->jsPathPublic . $str_filename;
        else
            $str_file = $this->jsPathPrivate . $str_filename;
        if( !in_array($str_file, $this->js) )
            $this->js[] = $str_file;
        return true;
	}

    /**
     * @param string $str_filename base on public path or private path setting
     * @param boolean $bool_isPublic default is false [base on private path]
     * @return true
     */
	public function addCSS($str_filename, $bool_isPublic=false){
        if( $bool_isPublic )
            $str_file = $this->cssPathPublic . $str_filename;
        else
            $str_file = $this->cssPathPrivate . $str_filename;
        if( !in_array($str_file, $this->css) )
            $this->css[] = $str_file;
        return true;
    }

   /**
    * @param string $str_metasetting meta string
    * @return true
    */
    public function addMeta($str_metasetting){
    	if( !in_array($str_metasetting, $this->meta) )
    	   $this->meta[] = $str_metasetting;
    	return true;
    }

    /**
     * @param string $str_title page tile
     * @return true
     */
    public function setPageTitle($str_title){
        require_once "../". $GLOBALS["local"] ."/"."header.php";
    	$this->title = $str_title . TITLEENDWORDTXT;
    	return true;
    }

    /**
     * @param string $str_title like style setting or javascript block
     * @param boolean $bool_rewrite insert model of rewrite model
     * @return true
     */
    public function setHeadExtends($str_string, $bool_rewrite = false ){
    	if( $bool_rewrite )
    	   $this->headExtends = $str_string;
    	else
    	   $this->headExtends.= $str_string;
    	return true;
    }

    /**
     * @return string
     */
    public function getJs(){
        return $this->getJsIncludes();
    }

    /**
     * @return string
     */
    public function getCss(){
        return $this->getCssIncludes();
    }

    /**
     * @return string
     */
    public function getMeta(){
        if( empty( $this->meta ) )
           return '';
        $str_result = '';
        foreach( $this->meta as $str_string )
            $str_result.= $str_string;
        return $str_string;
    }

    /**
     * @return string
     */
    public function getPageTitle(){
    	return $this->title;
    }

    /**
     * @return string
     */
    public function getHeadExtends(){
    	return $this->headExtends;
    }

    /**
     * @return string like &lt;head&gt;&lt;title&gt;title&lt;/title&gt;&lt;/head&gt;
     */
    public function getHead(){
    	$str_head = "<head>";
    	$str_head .= "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
    	$str_head .= "<title>{$this->title}</title>";
    	if( !empty( $str_extend ) )
    	   $str_head.= $str_extend;
    	$str_head.= $this->getMeta();
        $str_head.= $this->getJs();
    	$str_head.= $this->getCss();
    	$str_head.= $this->headExtends;
    	$str_head.= "</head>";
    	return $str_head;
    }

    /**
     * @return string like &lt;body&gt;hello world&lt;/body&gt;
     */
    public function getBody($str_content){
        require_once '../app/views/HeaderView.php';
        require_once '../app/views/FooterView.php';
    	return "<body>"
    	   . HeaderView::getCommonHeaderView()
    	   . $str_content
    	   . FooterView::getCommonFooterView()
           . '</body>';
    }

    /**
     * @return string like &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;
     */
    public function getHtmlPage($str_content){
    	return "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
             . "<html xmlns='http://www.w3.org/1999/xhtml'>"
             . $this->getHead()
             . $this->getBody($str_content)
             . "</html>";
    }

    public function getNavigation($arr_param){
        $str_result = "<div id='navigation'>"
                    . "<a href='/'>" . WEBSITEINDEXPAGETXT . "</a>";
        if (empty($arr_param))
            return $str_result . "</div>";
        foreach ($arr_param as $arr_array){
            if (empty($arr_array['url']))
                $str_result.= '&nbsp;&gt;&nbsp;' . $arr_array['text'];
            else
                $str_result.= "&nbsp;&gt;&nbsp;<a href='{$arr_array['url']}'>{$arr_array['text']}</a>";
        }
        return $str_result . '</div>';
    }

}
